import type { ChatMessage } from "@/types";
// import router from "@/router/index.ts";
// console.log(router);

export async function chatllm(message: ChatMessage, apiKey: string) {
  try {
    const result = await fetch("/api/v1/chat", {
      method: "post",
      // signal: AbortSignal.timeout(8000),
      // 开启后到达设定时间会中断流式输出
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${localStorage.getItem("token")}`,
      },
      body: JSON.stringify({
        "message": message.content,
        "history": []
      }),
    });
    return result;
  } catch (error) {
    throw error;
  }
}


export async function chatrag(message: ChatMessage, apiKey: string, diatance: number, database: string) {
  try {
    const result = await fetch("/api/v1/rag", {
      method: "post",
      // signal: AbortSignal.timeout(8000),
      // 开启后到达设定时间会中断流式输出
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${localStorage.getItem("token")}`,
      },
      body: JSON.stringify({
        "message": message.content,
        "history": [],
        diatance: diatance,
        database: database
      }),
    });

    return result

  } catch (error) {
    throw error;
  }
}

export async function chatragex(message: ChatMessage, apiKey: string, diatance: number, database: string) {
  try {
    const result = await fetch("/api/v1/excel_chat", {
      method: "post",
      // signal: AbortSignal.timeout(8000),
      // 开启后到达设定时间会中断流式输出
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${localStorage.getItem("token")}`,
      },
      body: JSON.stringify({
        "message": message.content,
        "history": [],
        diatance: diatance,
        database: database
      }),
    });

    return result

  } catch (error) {
    throw error;
  }
}

export async function chattool(message: ChatMessage, apiKey: string, obj: object) {
  try {
    const result = await fetch("/api/v1/tool", {
      method: "post",
      // signal: AbortSignal.timeout(8000),
      // 开启后到达设定时间会中断流式输出
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${localStorage.getItem("token")}`,
      },
      body: JSON.stringify({
        "message": message.content,
        "history": [],
        "tool": obj
      }),
    });

    return result

  } catch (error) {
    throw error;
  }

}
export async function chataisale(message: ChatMessage, apiKey: string, diatance: number) {
  try {
    const result = await fetch("/api/v1/ai_sale", {
      method: "post",
      // signal: AbortSignal.timeout(8000),
      // 开启后到达设定时间会中断流式输出
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${localStorage.getItem("token")}`,
      },
      body: JSON.stringify({
        "message": message.content,
        "history": [],
        diatance: diatance
      }),
    });

    return result

  } catch (error) {
    throw error;
  }
}
